cmake_minimum_required(VERSION 3.26)

# 语言环境配置 ###########################################################################################################
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 项目配置 ##############################################################################################################
project(janna_test VERSION 1.0.0 LANGUAGES CXX)

# qt 库 ################################################################################################################
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Test Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Test Widgets)

# vcpkg ################################################################################################################
set(vckpg_dir "/home/laolang/program/vcpkg")
set(CMAKE_TOOLCHAIN_FILE "${vckpg_dir}/scripts/buildsystems/vcpkg.cmake")
set(fmt_DIR "${vckpg_dir}/packages/fmt_x64-linux-dynamic/share/fmt")
set(spdlog_DIR "${vckpg_dir}/packages/spdlog_x64-linux-dynamic/share/spdlog")

# fmt 库
find_package(fmt CONFIG REQUIRED)
get_target_property(fmt_LOCATION fmt::fmt LOCATION)
get_filename_component(fmt_DIRECTORY ${fmt_LOCATION} DIRECTORY)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    string(REPLACE "debug/" "" fmt_DIRECTORY ${fmt_DIRECTORY})
endif()

message(STATUS "fmt_DIRECTORY:${fmt_DIRECTORY}")

# spdlog 库
find_package(spdlog CONFIG REQUIRED)
get_target_property(spdlog_LOCATION spdlog::spdlog LOCATION)
get_filename_component(spdlog_DIRECTORY ${spdlog_LOCATION} DIRECTORY)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    string(REPLACE "debug/" "" spdlog_DIRECTORY ${spdlog_DIRECTORY})
endif()

# 不需要 debug 日志时,注释下面两行
add_compile_options(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG)
add_compile_options(-DJANNA_LOG_LEVEL=1)

message(STATUS "spdlog_DIRECTORY:${spdlog_DIRECTORY}")

# rapidjson
find_path(RAPIDJSON_INCLUDE_DIRS "rapidjson/allocators.h")
message(STATUS "RAPIDJSON_INCLUDE_DIRS:${RAPIDJSON_INCLUDE_DIRS}")

# 项目变量配置 ###########################################################################################################

# 编译相关配置 ###########################################################################################################

# 生成 compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# 包含全局头文件
include_directories(${PROJECT_SOURCE_DIR}/include ${RAPIDJSON_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})

aux_source_directory(. test_srcs)
qt_standard_project_setup()
qt_add_executable(${PROJECT_NAME}
    MANUAL_FINALIZATION
    ${test_srcs}
)

set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${dist_dir}/test)
target_link_libraries(${PROJECT_NAME} PRIVATE
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Gui
    Qt${QT_VERSION_MAJOR}::Test
    Qt${QT_VERSION_MAJOR}::Widgets
)

# 测试相关配置 ###########################################################################################################
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD

    # testrapidjson
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/resources/plugins/plugin_desc.schema.json ${dist_dir}/test/plugin_desc.schema.json
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/resources/plugins/plugins.json ${dist_dir}/test/plugins.json
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/resources/plugins/plugins.schema.json ${dist_dir}/test/plugins.schema.json

    # COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/include/janna/plugins/taskbuttons/janna.taskbuttons.json ${dist_dir}/test/janna.taskbuttons.json
)

# 执行所有测试
# rm -rf build && cmake --preset=ninja-release && cmake --build --preset=ninja-release && ctest --test-dir build/ninja-release
add_test(NAME test_all COMMAND ${PROJECT_NAME})
set_tests_properties(test_all PROPERTIES WORKING_DIRECTORY ${dist_dir}/test)

# test add_custom_command
# rm -rf build && cmake --preset=ninja-release && cmake --build --preset=ninja-release && ctest --test-dir build/ninja-release -R test_common
add_test(NAME test_common COMMAND ${PROJECT_NAME} testCommon)
set_tests_properties(test_common PROPERTIES WORKING_DIRECTORY ${dist_dir}/test)